

State = {
		STOPPED : 0,
		RUNNING : 1,
		PAUSED : 2
}

Answer = {
		NONE  : 0,
		WRONG : 1,
		GOOD  : 2
}

var state = State.STOPPED;
var score = 0;
var time_left = 120;
var timer;
var problem;


$(document).ready(function() {
	problem = new TwoFactorMultiplication($("#question"));

	reset();

	$(this).keyup(function(event) {
		if (event.which == 32) {
				switch(state) {
				case State.RUNNING : 
					pause();
					break;

				case State.STOPPED : 
				case State.PAUSED : 
					resume();
					break;
				}				
		}
	});

	$("#answer").keyup(function() {
		if (state == State.RUNNING) {
			var answer = parseInt($(this).val())
			if (typeof answer != 'undefined' && !isNaN(answer)) {
				var increment = problem.check(answer);
				if (increment != 0) {
					setStatus((increment > 0) ? Answer.GOOD : Answer.WRONG)
					score += increment;
					if (score < 0) score = 0;
					$("#score").text(score);
					problem.next();
					$('#answer').val('');
				}
			}
		}
	});

	$("#start").click(function() {
		switch(state) {
		case State.STOPPED : 
			start();
			break;

		case State.RUNNING : 
			pause();
			break;

		case State.PAUSED : 
			resume();
			break;

		default : 
			error("invalid state: " + state);

		}

	});
	
	$("#stop").click(function() {
		if (state != State.STOPPED) {
			stop();
		}
	});

	reset();
});


function start() {
	reset();
	resume();
}

function pause() {
	$("#answer").attr('disabled', 'disabled');
	clearInterval(timer);

	// change symbol
	$("#start").removeClass("pause");
	$("#start").addClass("play");
	state = State.PAUSED;
	
	$("#paused").show();
}

function setStatus(answer) {
	switch(answer) {
	case Answer.NONE:
		$($(".mathdash-status")[0]).removeClass('good'); 
		$($(".mathdash-status")[0]).removeClass('oops'); 
		break;
	case Answer.GOOD:
		$($(".mathdash-status")[0]).addClass('good'); 
		$($(".mathdash-status")[0]).removeClass('oops'); 
		break;
	case Answer.WRONG:
		$($(".mathdash-status")[0]).removeClass('good'); 
		$($(".mathdash-status")[0]).addClass('oops'); 
		break;
	}
}

function resume() {
	if (time_left <= 0) {
		finish();
		return;
	}
	
	// load new problem
	problem.next();
	
	// change button
	$("#start").removeClass("play");
	$("#start").addClass("pause");

	// start timer
	timer = setInterval(function() {
		time_left--;
		if (time_left <= 0) {
			finish();
			return;
		} else {
			$("#time_left").text(time_left);
		}
	}, 1000);

	$("#time_left").text(time_left);
	$("#answer").val('');
	$("#answer").attr('disabled', false).focus();
	state = State.RUNNING;
	$("#paused").hide();
	
}

function finish() {
	if (timer) {
		clearInterval(timer);
		timer = null;
	}
	state = State.STOPPED;
	$("#answer").attr('disabled', 'disabled');
	$("#start").removeClass("pause");
	$("#start").addClass("play");
	
	display_results();
	
	$("#paused").hide();
}

function display_results(aborted) {
	$("#time_spent").text(120 - time_left);
	$("#questions_total").text(problem.questionsAnswered());
	$("#questions_correct").text(problem.questionsAnsweredCorrectly());
	$("#mathdash-running").hide(); 
	if (aborted) {
		$("#mathdash-gaveup").show();
		$("#mathdash-emoticon").addClass('negative');
	} else {
		$("#mathdash-goodwork").show();
		$("#mathdash-emoticon").addClass('positive');
	}
	$("#mathdash-results").show();
}

function stop() {
	if (timer) {
		clearInterval(timer);
		timer = null;
	}
	state = State.STOPPED;
	$("#answer").attr('disabled', 'disabled');
	$("#start").removeClass("pause");
	$("#start").addClass("play");
	
	// report results
	display_results(true);
	
	$("#paused").hide();
}

// reset all user interface elements in preparation of a new challenge.
function reset() {
	
	// kill timer if still running
	if (timer) {
		clearInterval(timer);
		timer = null;
	}

	// reset and show time
	time_left = 120;
	$("#time_left").text(time_left);
	$("#mathdash-running").show(); 
	$("#mathdash-goodwork").hide(); 
	$("#mathdash-gaveup").hide(); 
	$("#mathdash-results").hide();
	$("#mathdash-emoticon").removeClass('positive')
	$("#mathdash-emoticon").removeClass('negative')
	
	// reset score
	$("#score").text('0');
	score = 0;
	
	// reset problem
	problem.reset();
	setStatus(Answer.NONE);
	
	$("#paused").hide();
}


